package com.youlu.campus.service.visitor.impl;

import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.UserPointsCourseStudy;
import com.youlu.campus.entity.UserPointsDetail;
import com.youlu.campus.service.platform.PointPlatformConfigService;
import com.youlu.campus.service.ranking.UserPointRankingService;
import com.youlu.campus.service.visitor.UserPointsCourseStudyDetailService;
import com.youlu.campus.service.visitor.UserPointsCourseStudyService;
import com.youlu.campus.service.visitor.UserPointsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 平台级课程学习任务服务实现类
 */
@Service
@Slf4j
public class UserPointsCourseStudyServiceImpl implements UserPointsCourseStudyService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private PointPlatformConfigService pointPlatformConfigService;
    @Autowired
    private UserPointsService userPointsService;
    @Autowired
    private UserPointRankingService userPointRankingService;
    @Autowired
    private UserPointsCourseStudyDetailService userPointsCourseStudyDetailService;

    @Override
    public boolean startStudy(String userId, String platform, String appId) {
        if(this.isStudy(userId,platform,appId)){
            return true;
        }
        UserPointsCourseStudy userPointsCourseStudy = new UserPointsCourseStudy();
        userPointsCourseStudy.setUserId(userId);
        userPointsCourseStudy.setAppId(appId);
        userPointsCourseStudy.setPlatform(platform);
        mongoTemplate.insert(userPointsCourseStudy);
        return true;
    }

    @Override
    public boolean endStudy(UserPO userPO, String userId, String platform, String appId, String orderNo) {
        if(!this.isStudy(userId,platform,appId)){
            return false;
        }
        userPointsCourseStudyDetailService.create(userId,platform,appId,orderNo);
        Integer point = pointPlatformConfigService.getPoint(appId, "course-study");
        UserPointsDetail detail = new UserPointsDetail();
        detail.setUserId(userId);
        detail.setAction("add");
        detail.setBizDesc("课程学习");
        detail.setBizType("add");
        detail.setOptDate(new Date());
        detail.setPoints(point);
        detail.setComments("课程学习" + point + "积分");
        detail.setAppId(appId);
        detail.setPlatform(platform);
        userPointsService.addPoint(detail);
        userPointRankingService.updatePoint(userPO, "", point);
        return true;
    }

    @Override
    public boolean isStudy(String userId, String platform, String appId) {
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("platform").is(platform).and("appId").is(appId));
        return mongoTemplate.exists(query, UserPointsCourseStudy.class);
    }
}
